﻿namespace OpenCompiler.AST
{
	public class Statement : Node
	{
		internal Statement(Statement parent)
		{
			this.Parent = parent;
			if( null != parent )
			{
				parent.Body = this;
			}
		}

		public static FileStatement File(string filename)
		{
			var fs = new FileStatement(filename);
			return fs;
		}

		public Statement Parent { get; private set; }
		public Statement Body { get; private set; }

		private Statement GetRoot(Statement start)
		{
			if( null == start.Parent )
			{
				return start;
			}
			return this.GetRoot(start.Parent);
		}

		public Statement Root
		{
			get { return this.GetRoot(this); }
		}


		public string GenerateString()
		{
			return this.GenerateString(0, string.Empty, this);
		}

		protected string AddSpace(string input)
		{
			if( !string.IsNullOrEmpty(input))
			{
				input = input + " ";
			}
			return input;
		}

		private string GenerateString(int indentLevel, string baseString, Statement root)
		{
			var newString = "\n";
			for( var indent=0; indent<indentLevel; indent++ )
			{
				newString += "\t";
			}
			newString += root;
			if( null != root.Body )
			{
				indentLevel++;
				newString += GenerateString(indentLevel, baseString, root.Body);
				indentLevel--;
			}

			return newString;
		}


	}


	public class Statement<T> : Statement
		where T:class
	{
		internal Statement(Statement parent)
			: base(parent)
		{
		}
	}
}
